using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NMollom;

public partial class AllTests : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {

    }

    protected void GoButton_Click(object sender, EventArgs e) {
        Mollom mollom = new Mollom(ConfigurationManager.AppSettings["MollomPublicKey"],
                                   ConfigurationManager.AppSettings["MollomPrivateKey"]);

        // content checks first
        List<string> results = new List<string>();
        results.Add(Test("VerifyKeys",
            delegate() {
                return mollom.VerifyKey() ? "success" : "failure";
            }));
        results.Add(Test("All Servers",
            delegate() {
                return string.Join(", ", mollom.GetServerList());
            }));
        results.Add(Test("Check for Ham", 
            delegate() {
                return mollom.CheckContent("", "ham").ToString();                                   
            }));
        results.Add(Test("Check for Unsure",
            delegate() {
                return mollom.CheckContent("", "unsure").ToString();
            }));
        results.Add(Test("Check for Spam",
            delegate() {
                return mollom.CheckContent("", "spam").ToString();
            }));
        results.Add(Test("Sending feedback",
            delegate() {
                ContentAssesment ass = mollom.CheckContent("", "unsure");
                ass.SendFeedback(FeedbackType.Profanity);
                return "sessionId = " + ass.SessionId;
            }));
        results.Add(Test("Image captcha (correct)",
            delegate() {
                ContentAssesment ass = mollom.CheckContent("", "unsure");
                ImageCaptcha captcha = ass.GetImageCaptcha();
                return ass.ToString() + " <img src='" + captcha.Url + "'> = " + (captcha.Check("correct") ? "correct" : "incorrect");
            }));
        results.Add(Test("Audio captcha (incorrect)",
            delegate() {
                ContentAssesment ass = mollom.CheckContent("", "unsure");
                AudioCaptcha captcha = ass.GetAudioCaptcha();
                return ass.ToString() + " <a href='" + captcha.Url + "'>play</a> = " + (captcha.Check("incorrect") ? "correct" : "incorrect");
            }));
        results.Add(Test("Statistics",
            delegate() {
                Statistics stats = mollom.GetAllStatistics();
                return string.Format("TotalDays={0}, Total(a/r)={1}/{2}, Yesterday(a/r)={3}/{4}, Today(a/r)={5},{6}",
                                     stats.TotalDays, stats.TotalAccepted, stats.TotalRejected,
                                     stats.YesterdayAccepted, stats.YesterdayRejected,
                                     stats.TodayAccepted, stats.TodayRejected);
            }));

        Results.DataSource = results;
        Results.DataBind();
    }

    private delegate string TestMethod();
    private static string Test(string s, TestMethod tester) {
        try {
            return string.Concat(s, ": ", tester());
        }
        catch (Exception e) {
            return string.Concat("<span style='color: red;'>", s, ": ", e.ToString(), "</span>");
        }
    }
}
